package cn.itcast.wushangxiantu.Activity;

import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.LingShiGiveViewAdapter;
import cn.itcast.wushangxiantu.Fragment.LingShiZhuangbei;
import cn.itcast.wushangxiantu.Fragment.LingshiChongzhu;
import cn.itcast.wushangxiantu.UserData.UserBag;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LingShi extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout fl;
    private GridView gvLingShi;
    private TextView tvChongzhu;
    private TextView tvLingshi;
    private TextView tvFenJie;
    private LinearLayout ll;
    static int num = 1;
    static boolean active = true;
    UserBag userBag;
    LingShiGiveViewAdapter adapter;
    FragmentTransaction fragmentTransaction;
    private View viewChongzhu;
    private View viewLingshi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ling_shi);
        EventBus.getDefault().register(this);
        initView();
    }

    public static int getNum() {
        return num;
    }

    public static void setNum(int num) {
        LingShi.num = num;
    }

    public static boolean isActive() {
        return active;
    }

    public static void setActive(boolean active) {
        LingShi.active = active;
    }

    private void initView() {
        fl = (FrameLayout) findViewById(R.id.fl);
        gvLingShi = (GridView) findViewById(R.id.gvLingShi);
        tvChongzhu = (TextView) findViewById(R.id.tvChongzhu);
        tvLingshi = (TextView) findViewById(R.id.tvLingshi);
        ll = (LinearLayout) findViewById(R.id.ll);
        getLingshiData(Login.getUser().getUsernumber());
        tvChongzhu.setOnClickListener(this);
        tvLingshi.setOnClickListener(this);

        fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fl, new LingShiZhuangbei(LingShi.this));
        fragmentTransaction.commit();

        viewChongzhu = (View) findViewById(R.id.viewChongzhu);
        viewLingshi = (View) findViewById(R.id.viewLingshi);
    }

    private void getLingshiData(String usernumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + usernumber + "&need=beibao", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("beibao1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("beibao2", src);
                UserBag userBag = null;
                try {
                    userBag = new Gson().fromJson(src, UserBag.class);
                } catch (Exception e) {
                    userBag = null;
                }
                Message message = new Message();
                message.obj = userBag;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    userBag = (UserBag) message.obj;
                    if (null != userBag) {
                        if (adapter == null) {
                            adapter = new LingShiGiveViewAdapter(LingShi.this);
                        }
                        if (userBag != null) {
                            adapter.setLingshiBean(userBag.getBeibao().get(2));
                            gvLingShi.setAdapter(adapter);
                        }
                    } else {
                        ToastUtil.toast(LingShi.this, "服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });

    @Override
    public void onClick(View view) {
      viewLingshi.setVisibility(View.INVISIBLE);
      viewChongzhu.setVisibility(View.INVISIBLE);
        switch (view.getId()) {
            case R.id.tvChongzhu:
                viewChongzhu.setVisibility(View.VISIBLE);
                if (num != 0) {
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fl, new LingshiChongzhu(LingShi.this));
                    fragmentTransaction.commit();
                }
                num = 0;
                break;
            case R.id.tvLingshi:
                viewLingshi.setVisibility(View.VISIBLE);
                if (num != 1) {
                    fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.fl, new LingShiZhuangbei(LingShi.this));
                    fragmentTransaction.commit();
                }
                num = 1;
                break;
        }
    }

    /**
     * 接收后处理
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shuaxing(MessageEvent messageEvent) {
        if (messageEvent.getMessage().equals("Lingshi")) {
            getLingshiData(Login.getUser().getUsernumber());
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        num = 1;
        active = false;
    }
}
